package net.huashitong.common.controller;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.common.entity.SysAnnouncement;
import net.huashitong.common.filter.SysAnnouncementFilter;
import net.huashitong.common.service.AnnounceService;
import net.huashitong.common.service.SysAnnouncementService;
import net.huashitong.util.UtilsTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统  通告 控制
 * @author
 */
@Controller
@RequestMapping(value = "/common/announce")
public class SysAnnouncementController extends JsonBaseController {
    @Autowired
    private SysAnnouncementService sysAnnouncementService;
    @Autowired
    private AnnounceService announceService;
    @Override
    protected String getView(String s) {
        return "/modules/common/announce/"+s;
    }


    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid iniPageData(SysAnnouncementFilter filter) {
        List<SysAnnouncement> messageList = sysAnnouncementService.queryByFilter(filter);
        return coverJqGrid(filter,messageList);
    }
    @RequestMapping(method = RequestMethod.GET)
    public String initPage(Model model) {
        return getView("query");
    }

    @RequestMapping("/filter")
    public String filter(@ModelAttribute SysAnnouncementFilter filter) {
        return getView("filter");
    }
    /**********************           测试  流程             ****************************/
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String showEditView(Model model, @PathVariable String id) {
        SysAnnouncement sysMessage = sysAnnouncementService.getById(id);
        model.addAttribute("SysMessageFilter",sysMessage);
        return getView("form");
    }

    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Message update(@ModelAttribute SysAnnouncementFilter filter) {
        if (StringUtils.isBlank(filter.getSysId())) {
            return coverMessage(UtilsTools.ERROR,"通告不存在");
        }
        SysAnnouncement announcement = sysAnnouncementService.getById(filter.getSysId());
        if(ObjectUtils.isBlank(announcement)){
            return coverMessage(UtilsTools.ERROR,"通告不存在");
        }
        if (StringUtils.isNotBlank(announcement.getMsgType()) &&  !announcement.getMsgType().equals(filter.getMsgType())){
            return coverMessage(UtilsTools.ERROR,"通告类型不允许修改，请重新创建");
        }
        announcement = new SysAnnouncement();
        UtilsTools.copyPro(filter,announcement);
        sysAnnouncementService.update(announcement);
        return coverMessage(UtilsTools.CODE);
    }

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showAddView(@ModelAttribute SysAnnouncementFilter filter , Model model) {
        model.addAttribute("SysAnnouncementFilter",filter);
        return getView("form");
    }
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Message create(@ModelAttribute SysAnnouncementFilter filter) {
//        SysAnnouncement announcement = new SysAnnouncement();
//        UtilsTools.copyPro(filter,announcement);
//        sysAnnouncementService.save(announcement);
        return announceService.saveAnnoucement(filter);
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}")
    public Message softDelete(@PathVariable List<String> id){
        for (String sysid:id){
            SysAnnouncement announcement = sysAnnouncementService.getById(sysid);
            if(ObjectUtils.isBlank(announcement)){
                continue;
            }
            announcement.setDelFlag("1");
            sysAnnouncementService.update(announcement);
        }
        return coverMessage(UtilsTools.CODE);
    }

    @ResponseBody
    @RequestMapping(value = "/forceDelete/{id}")
    public Message forceDelete(@PathVariable List<String> id){
        return announceService.deleteForce(id);
    }
    /**********************           测试               ****************************/




}
